package com.dfbz.dao;

import com.dfbz.entity.Article;
import com.dfbz.entity.User;
import com.dfbz.utils.PageUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDao {
    public PageUtils<Map<String ,Object>> findArticleList(Integer cureePage, String title) {
        PageUtils<Map<String ,Object>> articlePageUtils = new PageUtils<>();
        Integer pageSize = articlePageUtils.getPageSize();
        String findAllSQL = "select a.*,count(f.a_id) collectCount from article a left join favorite f on" +
                " a.id = f.a_id where title like ? group by a.id order by publish_date desc limit ?,?";
        List<Map<String ,Object>> mapList = jdbcTemplate.queryForList(findAllSQL,"%"+title+"%",(cureePage-1)*pageSize,pageSize);
        Integer totalCount = jdbcTemplate.queryForObject("select count(1) from article where title like ?",Integer.class,"%"+title+"%");
        Integer pageCount = totalCount % pageSize == 0 ?totalCount / pageSize : (totalCount / pageSize) + 1 ;
        articlePageUtils.setCureePage(cureePage);
        articlePageUtils.setObjList(mapList);
        articlePageUtils.setPageCount(pageCount);
        return articlePageUtils;
    }

    public Article findArticleById(Long id) {
       return jdbcTemplate.queryForObject("select * from article where id = ?",new BeanPropertyRowMapper<>(Article.class),id);
    }

    public Long findCollectCountById(long aid) {
        return jdbcTemplate.queryForObject("select count(1) from favorite where a_id = ? ",Long.class,aid);
    }

    public void updateBrowseCountById(long aid) {
        jdbcTemplate.update("update article set browse_count = browse_count + 1 where id = ?",aid);
    }

    public List<User> findUsersByaid(long aid,long uid) {
        String sql = "select id,real_name,is_secret from `user` where id in\n" +
                "(select user_focus_id from userfocus where user_focus_id in (\n" +
                "select u_id from favorite where a_id = ?) and user_id = ?\n" +
                ")";
        try {
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(User.class),aid,uid);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public Integer IsAttentionWithMe(Long uid, long aid) {
        try {
            return jdbcTemplate.queryForObject("select count(1) from favorite where a_id = ? and u_id = ?",Integer.class,aid,uid);
        } catch (EmptyResultDataAccessException e) {
            return 0;
        }
    }

    public void removeCollectArticle(Long uid, long aid) {
        jdbcTemplate.update("delete from favorite where a_id = ? and u_id = ?",aid,uid);
    }


    public void collectArticle(Long uid, long aid) {
        jdbcTemplate.update("insert into favorite values (?,?) ",uid,aid);
    }

    public PageUtils<Map<String,Object>> findMyCollectById(Long uid, Integer cureePage, String title) {
        PageUtils<Map<String, Object>> articlePageUtils = new PageUtils<>();
        Integer pageSize = articlePageUtils.getPageSize();
        String findPageCountSQL = "select count(1) from article a \n" +
                "inner join favorite f on a.id = f.a_id\n" +
                "inner join user u on u.id  = f.u_id\n" +
                "where u.id = ? and a.title like ?";
        Integer totalCount = jdbcTemplate.queryForObject(findPageCountSQL,Integer.class,uid,"%"+title+"%");
        Integer pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize)+1;
        String findMapSQL = "select a.*,u.real_name from article a \n" +
                "inner join favorite f on a.id = f.a_id\n" +
                "inner join user u on u.id  = f.u_id\n" +
                "where u.id = ? and title like ? order by publish_date desc limit ?,?";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(findMapSQL, uid, "%" + title + "%", (cureePage - 1) * pageSize, pageSize);
        articlePageUtils.setCureePage(cureePage);
        articlePageUtils.setObjList(mapList);
        articlePageUtils.setPageCount(pageCount);
        return articlePageUtils;
    }

    public void addArticle(Article article) {
        jdbcTemplate.update("insert into article values(null,?,?,?,?,?,?)",
                article.getTitle(),
                article.getContent(),
                article.getBrowseCount(),
                article.getPublishDate(),
                article.getPublishRealName(),
                article.getUserId()
        );
    }
}
